package com.fd.dx.service.impl;

import com.fd.dx.domain.ZP;
import com.fd.dx.mapper.ZPMapper;
import com.fd.dx.service.ZPService;
import com.fd.dx.web.domain.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2018/2/26.
 */
@Service
public class ZPServiceImpl implements ZPService{
    @Autowired
    public ZPMapper zpMapper;

    @Override
    public List<ZP> getChildfromPid(Integer pid) {
        return zpMapper.getChildfromPid(pid);
    }

    /**
     * 获得家族目前一共有多少代
     * @return
     */
    @Override
    public Object getMaxGene() {
        List ls=zpMapper.daList();
        return ls.get(ls.size()-1);
    }

    @Override
    public ZP finePersonFromID(Integer id) {
        return zpMapper.finePersonFromID(id);
    }
    @Override
    public Page<ZP> getPage(Page<ZP> page) {
        Page<ZP> result=new Page<ZP>();
        result.setTotal(zpMapper.getTotalCount());
        result.setRows(zpMapper.getDataFromPage(page));
        return result;
    }

    @Override
    public ZP getParentfromPid(Integer pid) {
        return zpMapper.getParentfromPid(pid);
    }

    @Override
    public List<ZP> getXD(Map map) {
        return zpMapper.getXD(map);
    }

    public Page<ZP> finePersonFromName(String name,Page<ZP> page) {
        Page<ZP> result=new Page<ZP>();
        result.setTotal(zpMapper.getTotalCountFromName(name));
        Map map=new HashMap();
        map.put("name",name);
        map.put("page",page);
        result.setRows(zpMapper.getDataFromPageAndName(map));
        return result;
    }

    @Override
    public List<ZP> getTXD(Integer pid) {
        ZP parent=zpMapper.finePersonFromID(pid);
        Integer ppid=parent.getPid();
        Map map=new HashMap();
        map.put("pid",ppid);
        map.put("id",pid);
        List <ZP> zps=zpMapper.getXD(map);
        List<ZP> result=new ArrayList<>();
        for(ZP z : zps)
        {
            List<ZP> temp= zpMapper.getChildfromPid(z.getId());
            result.addAll(temp);
        }
        return result;
    }

    @Override
    public Page<ZP> finePersonFromRName(String rname, Page<ZP> page) {
        Page<ZP> result=new Page<ZP>();
        result.setTotal(zpMapper.getTotalCountFromRName(rname));
        Map map=new HashMap();
        map.put("rname",rname);
        map.put("page",page);
        result.setRows(zpMapper.getDataFromPageAndRName(map));
        return result;
    }

    @Override
    public Page<ZP> finePersonFromIDName(String idname, Page<ZP> page) {
        Page<ZP> result=new Page<ZP>();
        result.setTotal(zpMapper.getTotalCountFromIDName(idname));
        Map map=new HashMap();
        map.put("idname",idname);
        map.put("page",page);
        result.setRows(zpMapper.getDataFromPageAndIDName(map));
        return result;
    }

    @Override
    public List<ZP> getZPL(Integer pid) {
        List<ZP> zps=new ArrayList<>();
        createZPL(zps,pid);
        return zps;
    }

    @Override
    public Integer hdtt(Integer id) {
        Map<String,Integer> map=new HashMap<String,Integer>();
        map.put("sum",0);
        chdtt(map,id);
        return map.get("sum");
    }

    @Override
    public Integer addZP(ZP zp) {
        zp.setId(zpMapper.getId());
        return zpMapper.addZP(zp);
    }

    @Override
    public Integer updateZP(ZP zp) {
        return zpMapper.updateZP(zp);
    }

    @Override
    public Integer deleteZP(Integer id) {
        return zpMapper.deleteZP(id);
    }

    private void chdtt(Map<String,Integer> map,Integer id){
        List<ZP> zps=zpMapper.getChildfromPid(id);
        if(zps.size()>0)
        {
            map.put("sum",map.get("sum")+zps.size());
            for(ZP z : zps){
                chdtt(map,z.getId());
            }
        }
    }
    private void createZPL(List<ZP> zps,Integer id){
        ZP zp=zpMapper.finePersonFromID(id);
        zps.add(zp);
        if(zp.getPid()!=0)
        {
            createZPL(zps,zp.getPid());
        }
    }
}
